package jonaVisual;

import Jona.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class VisualArgObject implements VisualObject {
	private ArgObject a1;
	private Rectangle r1;
	private Dimension d1;
	//private Rectangle text_r;
	private int textLength = 0;
	private int lines =0;
	private String oldText="";
	private static int text_indentX = 42;
	private static int text_indentY = 5;
	private static int spacing_X = 10;
	private int desired_width = 0;
	static final long serialVersionUID = 123123743;
	private int lineWidth = 1;
	private boolean drawText = true;
	private boolean isRecursive = false;
	private int evaluation;
	//private JLabel jl1;
	
	Font italicFont = new Font ("Arial",Font.ITALIC,12);
	Font normalFont = new Font ("Arial",Font.PLAIN,12);
	Font boldFont = new Font ("Arial", Font.BOLD,12);
	Font strikeFont = new Font ("Arial", Font.BOLD, 12);
	
	public VisualArgObject(ArgObject a1)
	{	
		
		this.a1 = a1;	
	}

	public int getLines() { return lines;}
	
	public int updateEvaluation(Theory t1)
	{
		evaluation = a1.evaluate(t1);
		return evaluation;
	}
	
	public void setRecursive(boolean recursive)
	{
		isRecursive = recursive;
	}
	
	public boolean isRecursive() { return isRecursive; }
	public Rectangle getBounds() {return getRectangle();}
	public void setDrawText(boolean drawText) { this.drawText = drawText;}
	
	public ArgObject getArgObject() {return a1;}
	
	public void translate(int x,int y)
	{
		if(r1 != null)
		{
			r1.translate(x,y);
		}
	}
	public int getTextIndentX() {return text_indentX;}
	public void doHighlight() { lineWidth =2;}
	public void deHighLight() { lineWidth =1;}
	public boolean isHighlighted()
	{
		return(lineWidth==2);
	}
	
	public void clicked()
	{
		if(lineWidth== 1)lineWidth = 2;
		else lineWidth = 1;
	}
	
	public boolean contains(int x, int y)
	{
		return contains(new Point(x,y));
	}
	public boolean contains(Point p)
	{
		return r1.contains(p);
	}
	
	public void newTextSet()
	{
		d1 = null;
	}
	
	public void setRectangle(Rectangle r1)
	{
		this.r1 = r1;
		desired_width = r1.width;
	}
	
	public Rectangle getRectangle() {return r1;}
	
	public void draw(Graphics2D g2, Theory t1)
	{
		//int type = a1.getArgumentType();
		int type = Statement.ASSUMPTION;
		System.out.println("Evaluation VisArgObject " + a1.getName() + ": " + evaluation + ". Type: " + type);
		//Graphics2D g2 = (Graphics2D)g;
		//verhouding van 2,5 
		// 5,4,5,4 bij 100 40
		
		//float verh = r1.width / r1.height;
	
		String text = "";
		if(a1 != null)
		{
			type = a1.getArgumentType();
			// Add type of Statement
		    if (type == Statement.ISSUE)
		    	text += "? ";
		    else if(type == Statement.ASSUMPTION)
		    	text += "! ";
   		
			text += a1.getText();
			if(isRecursive)
				text += "(Recursive Path)";
			
		}
		
			if(type == Statement.ISSUE)
			{
				float[] dashPattern = { 5,4,5,4};
		
				g2.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_BUTT,
	                                  		BasicStroke.JOIN_MITER, 10,
	                                  		dashPattern, 0));
			}
			else
			{
				g2.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER , 10,null,0));
			}
			
			if(r1 == null || d1 == null || text.length()  != textLength)
			{
				if(text.length()  != textLength) r1.width=0;
				Rectangle2D r2 = null;
				String[] data = text.split("\n");
				lines = data.length;
				String line = "";
				double width1=0;
				int height1 =0;
				if(data.length > 1)
				{
					double dumx =0;
					for(int i =0; i < data.length; i++)
					{
						line = data[i];
						r2 = g2.getFontMetrics().getStringBounds(line,0,line.length(),g2 );	
						dumx = r2.getWidth();
						if(dumx > width1) width1 = dumx;
					}
					height1 += ((data.length - 1) * 10) + 20;
				}
				else
				{
					r2 = g2.getFontMetrics().getStringBounds(text,0,text.length(),g2 );	
					width1 = r2.getWidth();	
					if(desired_width < 100) desired_width=100;
					if(width1 < desired_width) width1 = desired_width;
				}
				if(height1 < 40) height1 = 40;
				int newWidth = (int)(width1 + spacing_X + text_indentX);
				if(newWidth < r1.width) newWidth = r1.width; 
				r1 =  new Rectangle(r1.x,r1.y,newWidth ,height1);				
				d1 = new Dimension((int)(width1 /(4 * (width1 + 10) / 150)),height1 /2 );
				//text_r = r2.getBounds();
				//System.out.println(text_r.x+ " " + text_r.y + " " + text_r.width + " " + text_r.height);
				textLength = text.length();
			}
		
			g2.setColor(Color.white);	
			
	    	g2.fillRoundRect(r1.x,r1.y,r1.width,r1.height ,d1.width,d1.height);
	    	
	        if (evaluation == a1.EVALUATION_UNKNOWN)
	        	g2.setColor(Color.cyan);
	        else if (evaluation == a1.EVALUATION_UNDEFEATED)
	        	g2.setColor(Color.blue);
	        else if (evaluation == a1.EVALUATION_DEFEATED)
	        	g2.setColor(Color.red);
	        else if (evaluation == a1.EVALUATION_UNASSIGNED)
	        	g2.setColor(Color.green);
	        else 
	        	g2.setColor(Color.LIGHT_GRAY);
	    	//g2.setColor(Color.black);
	    	g2.drawRoundRect(r1.x,r1.y,r1.width,r1.height ,d1.width,d1.height);	
	    	if (evaluation == a1.EVALUATION_UNKNOWN)
			{
				g2.setFont (italicFont);
				g2.setColor(Color.cyan);
			}
	    	else if (evaluation == a1.EVALUATION_UNDEFEATED)
	    	{
	    		g2.setFont(boldFont);
	    		g2.setColor(Color.blue);
	    	}
	    	else if (evaluation == a1.EVALUATION_DEFEATED)
	    	{
	    		g2.setFont(boldFont);
	    		g2.setColor(Color.red);
	    	}
	    	else if (evaluation == a1.EVALUATION_UNKNOWN)
	    	{
	    		g2.setFont(boldFont);
	    		
	    		g2.setColor(Color.red);
	    	}
	    	if(drawText)
	    	{
	    		String[] data = text.split("\n");
	    		if(data.length > 1)
				{
	    			for(int i =0; i < data.length ; i++)
	    			{
	    				g2.drawString(data[i],r1.x + text_indentX,r1.y + (i*10) + text_indentY + 10);
	    			}
	    		}
	    		else
	    		{
	    			g2.drawString(text,r1.x + text_indentX,r1.y + (r1.height/2) + text_indentY);
	    		}
	    	}
	    	//System.out.println(text + " " + "eval: " + evaluation);
	    }

}
